package com.strange.mdl.module.mdl.controller.admin.debatehall;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.strange.mdl.framework.common.pojo.PageParam;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.pojo.CommonResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;
import static com.strange.mdl.framework.common.pojo.CommonResult.success;

import com.strange.mdl.framework.excel.core.util.ExcelUtils;

import com.strange.mdl.framework.apilog.core.annotation.ApiAccessLog;
import static com.strange.mdl.framework.apilog.core.enums.OperateTypeEnum.*;

import com.strange.mdl.module.mdl.controller.admin.debatehall.vo.*;
import com.strange.mdl.module.mdl.dal.dataobject.debatehall.DebatehallDO;
import com.strange.mdl.module.mdl.service.debatehall.DebatehallService;

@Tag(name = "管理后台 - 辨经殿话题")
@RestController
@RequestMapping("/mdl/debatehall")
@Validated
public class DebatehallController {

    @Resource
    private DebatehallService debatehallService;

    @PostMapping("/create")
    @Operation(summary = "创建辨经殿话题")
    @PreAuthorize("@ss.hasPermission('mdl:debatehall:create')")
    public CommonResult<Long> createDebatehall(@Valid @RequestBody DebatehallSaveReqVO createReqVO) {
        return success(debatehallService.createDebatehall(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新辨经殿话题")
    @PreAuthorize("@ss.hasPermission('mdl:debatehall:update')")
    public CommonResult<Boolean> updateDebatehall(@Valid @RequestBody DebatehallSaveReqVO updateReqVO) {
        debatehallService.updateDebatehall(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除辨经殿话题")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdl:debatehall:delete')")
    public CommonResult<Boolean> deleteDebatehall(@RequestParam("id") Long id) {
        debatehallService.deleteDebatehall(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除辨经殿话题")
                @PreAuthorize("@ss.hasPermission('mdl:debatehall:delete')")
    public CommonResult<Boolean> deleteDebatehallList(@RequestParam("ids") List<Long> ids) {
        debatehallService.deleteDebatehallListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得辨经殿话题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdl:debatehall:query')")
    public CommonResult<DebatehallRespVO> getDebatehall(@RequestParam("id") Long id) {
        DebatehallDO debatehall = debatehallService.getDebatehall(id);
        return success(BeanUtils.toBean(debatehall, DebatehallRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得辨经殿话题分页")
    @PreAuthorize("@ss.hasPermission('mdl:debatehall:query')")
    public CommonResult<PageResult<DebatehallRespVO>> getDebatehallPage(@Valid DebatehallPageReqVO pageReqVO) {
        PageResult<DebatehallDO> pageResult = debatehallService.getDebatehallPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DebatehallRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出辨经殿话题 Excel")
    @PreAuthorize("@ss.hasPermission('mdl:debatehall:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDebatehallExcel(@Valid DebatehallPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DebatehallDO> list = debatehallService.getDebatehallPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "辨经殿话题.xls", "数据", DebatehallRespVO.class,
                        BeanUtils.toBean(list, DebatehallRespVO.class));
    }

}